package org.nekperu15739.boot.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.nekperu15739.boot.dao.ClienteDao;
import org.nekperu15739.boot.dao.ContinentDao;
import org.nekperu15739.boot.dao.CountryDao;
import org.nekperu15739.boot.dao.FacturaDao;
import org.nekperu15739.boot.domain.Cliente;
import org.nekperu15739.boot.domain.Continent;
import org.nekperu15739.boot.domain.Country;
import org.nekperu15739.boot.domain.Factura;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {

	@Autowired
	private DataSource dataSource;

	@Autowired
	private ContinentDao continentDao;

	@Autowired
	private CountryDao countryDao;

	@Autowired
	private FacturaDao facturaDao;

	@Autowired
	private ClienteDao clienteDao;

	private Continent continent;
	private Country country;

	@RequestMapping("/work")
	@ResponseBody
	public String index2() {
		Cliente c = new Cliente(0, "Eugenio Cangrejo", 444, true, new Date());

		// Presentaciones en los dos sentidos...
		List<Factura> facturas = new ArrayList<Factura>();
		facturas.add(new Factura(0, "FAC-1", "FP1", c));
		facturas.add(new Factura(0, "FAC-2", "FP2", c));
		facturas.add(new Factura(0, "FAC-3", "FP3", c));
		facturas.add(new Factura(0, "FAC-4", "FP4", c));
		facturas.add(new Factura(0, "FAC-5", "FP5", c));

		c.setFacturas(facturas);

		Cliente save = clienteDao.save(c);
		System.out.println(save.toString());

		return save.toString();
	}

	@RequestMapping("/")
//	@ResponseBody
	public String index3() {
		Cliente c = new Cliente("Eugenio Cangrejo", 444, true, new Date());

		// Presentaciones en los dos sentidos...
		List<Factura> facturas = new ArrayList<Factura>();
		facturas.add(new Factura("FAC-1", "FP1", c));
		facturas.add(new Factura("FAC-2", "FP2", c));
		facturas.add(new Factura("FAC-3", "FP3", c));
		facturas.add(new Factura("FAC-4", "FP4", c));
		facturas.add(new Factura("FAC-5", "FP5", c));

		c.setFacturas(facturas);

		Cliente save = clienteDao.save(c);
		System.out.println(save.toString());

//		return save.toString();
		return "hola";
	}

	@RequestMapping("/2")
	@ResponseBody
	public String index() {
		country = new Country();
		country.setName("peru");
		country.setCountryId((long) 0);

		Country country2 = new Country();
		country2.setName("chile");

		// Country countrySaved = countryDao.save(country);
		// System.out.println(countrySaved);

		continent = new Continent();
		continent.setContinent("america");
		List<Country> countries = new ArrayList<Country>();
		countries.add(country);
		countries.add(country2);

		continent.setCountries(countries);
		continent.setContinentId((long) 0);

		Continent continentSaved = continentDao.save(continent);
		System.out.println(continentSaved);

		return continentSaved.toString();
	}
}
